
from . import ConnPG
import psycopg2


class SelectUsers:

    async def select_all_users(self, page='1', limit='20'):#查看所有用户信息

        #参数检查
        if not (page.isdecimal() and limit.isdecimal()):

            return {
                'status': -1,
                'errorCode': 105100,
                'errorMsg': '参数必须为整数类型',
                'resultBody': None
            }

        page, limit = (int(page)-1)*int(limit), int(limit)

        sql = 'select * from view_user_duty limit %s offset %s'

        result = await ConnPG().fetchall(sql, (limit, page))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def select_all_users_by_users_table(self, page='1', limit='20'):#查看所有用户信息

        #参数检查
        if not (page.isdecimal() and limit.isdecimal()):

            return {
                'status': -1,
                'errorCode': 105100,
                'errorMsg': '参数必须为整数类型',
                'resultBody': None
            }

        page, limit = (int(page)-1)*int(limit), int(limit)

        sql = 'select * from users limit %s offset %s'

        result = await ConnPG().fetchall(sql, (limit, page))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }
    
    async def select_user_id_info(self, user_id):#查看指定user_id的信息

        #参数检查
        if not user_id:

            return {
                'status': -1,
                'errorCode': 105100,
                'errorMsg': '请检查参数，需要要一个用户的ID',
                'resultBody': None
            }

        if not user_id.isdecimal():

            return {
                'status': -1,
                'errorCode': 105100,
                'errorMsg': '参数必须为整数类型',
                'resultBody': None
            }

        user_id = int(user_id)

        sql = 'select * from view_user_duty where id=%s'

        result = await ConnPG().fetchall(sql, (user_id,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def select_user_phone_info(self, phone):#查看指定user_id的人员信息

        #参数检查
        if not phone:

            return {
                'status': -1,
                'errorCode': 105108,
                'errorMsg': '请检查参数，需要要一个用户的电话号码',
                'resultBody': None
            }

        if not phone.isdecimal() or len(phone) != 11:

            return {
                'status': -1,
                'errorCode': 105110,
                'errorMsg': '参数必须为整数类型,且长度为11位的电话号码',
                'resultBody': None
            }


        sql = 'select * from view_user_duty where phone=%s'

        result = await ConnPG().fetchall(sql, (phone,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }
    
    async def select_user_duty_info(self, duty):#查看指定职务的人员信息

        #参数检查
        if not duty:

            return {
                'status': -1,
                'errorCode': 105111,
                'errorMsg': '请检查参数，需要职务',
                'resultBody': None
            }


        sql = 'select * from view_user_duty where duty_name=%s'

        result = await ConnPG().fetchall(sql, (duty,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def select_user_name_info(self, name):#查看指定职务的人员信息

        #参数检查
        if not name:

            return {
                'status': -1,
                'errorCode': 105112,
                'errorMsg': '请检查参数，需要姓名',
                'resultBody': None
            }


        sql = 'select * from view_user_duty where name=%s'

        result = await ConnPG().fetchall(sql, (name,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    